import { queryArticleDetail } from "@/api/content/article";
import { Article } from "@/types/content/article";
import { useCallback, useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";


export const useArticleDetail = () => {
    const [searchParams] = useSearchParams();
    const id = searchParams.get('id')||'';
    const [loading,setLoading] = useState(false)
    // 文章详情
    const [articleInfo,setArticleInfo] = useState<Article.Row>({
        id: "",
        title: "",
        summary: "",
        categoryId: "",
        categoryName: "",
        content: "",
        thumbnail: "",
        viewCount: "",
        codeTheme: "",
        createTime: "",
        tagList: [],
    });

 
    const getArticleDdetail = useCallback(async () => {
        if(!id) return
        setLoading(true)
        const res = await queryArticleDetail(id);
        setLoading(false)
        if (res.code === 200) {
            setArticleInfo(res.data)
        }
    },[id])
    useEffect(()=>{
        getArticleDdetail();
    },[getArticleDdetail])
   
    return {
        articleInfo,
        loading
    }
}